package com.xk.meeting.back.controller;

import com.xk.meeting.entity.Message;
import com.xk.meeting.service.*;
import com.xk.meeting.utils.UtilHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/back/changeStatus")
public class ChangeStatusServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();

        String id = request.getParameter("id");
        String status = request.getParameter("status");

        String referer = request.getHeader("referer");

        MessageService messageService = new MessageServiceImpl();
        UserService userService = new UserServiceImpl();
        ApplyService applyService = new ApplyServiceImpl();
        Message message = new Message();
        int count;
        if (referer.contains("regformList")){
            count = userService.changeStatus(Integer.parseInt(id),Integer.parseInt(status));
            if(status.equals("1")){
                String username = userService.getNameById(Integer.parseInt(id));
                message.setUid(Integer.parseInt(id));
                message.setTitle("系统消息");
                message.setContent("尊敬的用户"+username+",您好!您的会议室登记信息以通过,现在可以进行会议室预约,祝您使用愉快");
                messageService.addMessage(message);
            }
        }else {
            count = applyService.changeStatus(Integer.parseInt(id),Integer.parseInt(status));
            if(status.equals("1")){
                int uid = applyService.getUid(Integer.parseInt(id));
                String username = userService.getNameById(uid);
                String meetingName = applyService.getMeetingName(Integer.parseInt(id));
                String DateTime = applyService.getDateTime(Integer.parseInt(id));
                message.setUid(uid);
                message.setTitle("系统消息");
                message.setContent("尊敬的用户"+username+",您好!您预约于"+DateTime+meetingName+"号会议室已经预约通过,请您注意时间,准时到达.");
                messageService.addMessage(message);
            }
        }
        if (count > 0){
            out.print("ok");
        }else {
            out.print("cancel");
        }

    }
}
